package com.ss.android.article.browser.main;

import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.ss.android.article.browser.BrowserPage;
import com.ss.android.article.browser.FeedFragment;
import com.ss.android.article.browser.api.BrowserLFApi;
import com.ss.android.article.browser.main.bean.WeatherSourceBean;
import com.ss.android.article.browser.website.NavigationBean;
import com.ss.android.browser.base.wrapper.net.RetrofitWrapper;
import com.ss.android.browser.base.wrapper.net.c;

import java.util.ArrayList;
import java.util.List;

public final class BrowserPresenter {
    public final BrowserPage mBrowserPage;
    protected TabInspector mTabInspector;
    protected List<NavigationBean> mNavigationBeanList;
    private FeedFragment mFeedFragment;

    public BrowserPresenter(@NonNull BrowserPage browserPage) {
        this.mNavigationBeanList = new ArrayList();
        this.mBrowserPage = browserPage;
        TabInspector.setOnTabNumberChangedListener(new TabInspector.OnTabNumberChangedListener() {
            public final void tabNumberChanged(int tabNumber) {
                BrowserPresenter.this.mBrowserPage.updateTabNumber(tabNumber);
            }
        });
    }

    public synchronized final void tabChanged(int position) {
        Log.d("BrowserPresenter", "tabChanged: " + position);
        if (position >= 0 && position < this.mTabInspector.tabSize()) {
            FeedFragment feedFragment = this.mTabInspector.switchToTab(position);
            Log.d("BrowserPresenter", "On tab changed");
            if (feedFragment == null) {
                this.mBrowserPage.removeTabView();
                if (this.mFeedFragment != null) {
                    this.mFeedFragment.f();
                    this.mFeedFragment.e();
                }
            } else {
                feedFragment.g();
                feedFragment.h();
                this.mBrowserPage.updateProgress(feedFragment.i());
                this.mBrowserPage.updateUrl(feedFragment.getUrl(), false);
                this.mBrowserPage.setTabView(feedFragment);
                if (this.mTabInspector.a(feedFragment) >= 0) {
                    this.mBrowserPage.notifyTabViewChanged(this.mTabInspector.a(feedFragment));
                }
            }
            this.mFeedFragment = feedFragment;
        }
    }

    public final void a(final Handler handler) {
        RetrofitWrapper.getISRetrofit().create(BrowserLFApi.class).getWeather().enqueue(new c() {

            public final void a(Object arg3) {
                if ((TextUtils.equals("success", ((WeatherSourceBean) arg3).message))
                        && ((WeatherSourceBean) arg3).data != null
                        && ((WeatherSourceBean) arg3).data.weather != null
                        && BrowserPresenter.this.mBrowserPage != null
                        && !((ArticleMainActivity) BrowserPresenter.this.mBrowserPage).isFinishing()) {
                    Message message = Message.obtain();
                    message.obj = ((WeatherSourceBean) arg3).data.weather;
                    message.what = 10;
                    handler.sendMessage(message);
                }
            }

            public final void b(Throwable arg2) {
                android.support.design.a.d(arg2.getMessage());
            }
        });
    }
}

